<%@ page language="java" contentType="text/html;charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<c:choose>
	<c:when test="${!empty project }">
		<div class="page-header">
			<h3 style="margin-top: auto">
				<c:out value="${project.name}"></c:out>
			</h3>
		</div>

		<div class="">
		      <h4>导入系统功能模型</h4>
			<table class="table table-hover">
				<tr>

					<td>
						<form class="form-horizontal" id="import-rhapsody-form"
							action="<%=request.getContextPath()%>/rhapsody/${project.id}/upload"
							method="POST" enctype="multipart/form-data"
							onsubmit="return valid()">

							<div class="control-group" id="rhapsody_file_div">
								<label class="control-label">上传模型文件</label>
								<div class="controls">
									<input type="file" class="file-inputs" title="选择rhapsody文件"
										name="rhapsody_file">
									<button type="submit" class="btn " id="submit">
										<i class="icon-ok"></i>&nbsp;上传
									</button>
									<span class="help-inline" id="rhapsody_file_help"></span>
								</div>
							</div>
						</form>
						<div class="form-horizontal">
							<div class="control-group">
								<label class="control-label">模型文件</label>
								<div class="controls">
									<c:choose>
										<c:when test="${!empty project.rhapsodyFile }">
										    <button class="btn " name="" onclick="mapRhapsodyFile(${project.id})">
										      <i class="icon-zoom-in"></i>&nbsp;解析
										    </button>
										</c:when>
										<c:otherwise>
											<c:out value="请上传模型文件"></c:out>
										</c:otherwise>
									</c:choose>
									
								</div>
							</div>
						</div>
					</td>
				</tr>
			</table>
		</div>
		
		
		<div class="span9">
            <h4>实体数据区</h4>
            <table class="table table-hover table-striped table-bordered">
                <thead>
                    <tr>
                        <th style="width:10%">#</th>
                        <th style="width:25%">名称</th>
                        <th style="width:15%">大小</th>
                        <th style="width:25%">实体</th>
                        <th style="width:25%">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <c:forEach items="${entityZones}" var="tuple"
                        varStatus="status">
                        <tr>
                            <td>${status.count}</td>
                            <td>${tuple.first.name}</td>
                            <td>${tuple.first.size}</td>
                            <c:choose>
                                <c:when test="${!empty tuple.second }">
                                     <td>${tuple.second.name}</td>
                                     <td>
                                        <button class="btn" name="" onclick="showEntityZoneAddPanel('${tuple.first.entityUri}', '${tuple.first.name}', ${tuple.first.size}, ${project.id})">
                                            <i class="icon-plus"></i>&nbsp;增加
                                        </button>
                                     </td>
                                </c:when>
                                <c:otherwise>
                                     <td><c:out value="Not Found"></c:out></td>
                                     <td>
                                        <button class="btn btn-primary disabled">
                                            <i class="icon-plus icon-white"></i>&nbsp;增加
                                        </button>
                                     </td>
                                </c:otherwise>
                            </c:choose>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>
        
        <div class="span9">
            <h4>总线数据区</h4>
            <table class="table table-hover table-striped table-bordered">
                <thead>
                    <tr>
                        <th style="width:10%">#</th>
                        <th style="width:25%">名称</th>
                        <th style="width:25%">实体</th>
                        <th style="width:25%">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <c:forEach items="${busZones}" var="busZone"
                        varStatus="status">
                        <tr>
                            <td>${status.count}</td>
                            <td>${busZone.name}</td>
                            <td></td>
                            <td></td>
                            <%-- <c:choose>
                                <c:when test="${!empty tuple.second }">
                                     <td>${tuple.second.name}</td>
                                     <td>
                                        <button class="btn" name="" onclick="addEntityZone('${tuple.first.entityUri}', '${tuple.first.name}', ${tuple.first.size}, ${project.id})">
                                            <i class="icon-plus"></i>&nbsp;增加
                                        </button>
                                     </td>
                                </c:when>
                                <c:otherwise>
                                     <td><c:out value="Not Found"></c:out></td>
                                     <td>
                                        <button class="btn btn-primary disabled">
                                            <i class="icon-plus icon-white"></i>&nbsp;增加
                                        </button>
                                     </td>
                                </c:otherwise>
                            </c:choose> --%>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>
		
		
		
		<div class="span9">
            <h4>显示数据区</h4>
            <table class="table table-hover table-striped table-bordered">
                <thead>
                    <tr>
                        <th style="width:10%">#</th>
                        <th style="width:25%">名称</th>
                        <th style="width:15%">大小</th>
                        <th style="width:25%">实体</th>
                        <th style="width:25%">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <c:forEach items="${displayZones}" var="tuple"
                        varStatus="status">
                        <tr>
                            <td>${status.count}</td>
                            <td>${tuple.first.name}</td>
                            <td>${tuple.first.size}</td>
                            <c:choose>
                                <c:when test="${!empty tuple.second }">
                                     <td>${tuple.second.name}</td>
                                     <td>
                                        <button class="btn" onclick="addDisplayZone('${tuple.first.entityUri}', '${tuple.first.name}', ${tuple.first.size}, ${project.id})">
                                            <i class="icon-plus"></i>&nbsp;增加
                                        </button>
                                     </td>
                                </c:when>
                                <c:otherwise>
                                     <td><c:out value="Not Found"></c:out></td>
                                     <td>
                                        <button class="btn btn-primary disabled" name="">
                                            <i class="icon-plus icon-white"></i>&nbsp;增加
                                        </button>
                                     </td>
                                </c:otherwise>
                            </c:choose>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>
        
        <div class="span9">
            <h4>总线通信代理</h4>
            <table class="table table-hover table-striped table-bordered">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>代理名称</th>
                        <th>总线数据区</th>
                        <th>外部系统</th>
                        <th>响应函数</th>
                        <th>响应函数</th>
                        <th>响应函数</th>
                        <th>响应函数</th>
                    </tr>
                </thead>
                <tbody>
                    <c:forEach items="${busAgents}" var="busAgent"  varStatus="status">
                        <tr>
                            <td>${status.count}</td>
                            <td>${busAgent.name}</td>
                            <td>${busAgent.busZone}</td>
                            <td>${busAgent.outerSystem}</td>
                            <c:choose>
                                <c:when test="${!empty busAgent.readInitEvent }">
                                     <td>${busAgent.readInitEvent.name} </td>
                                </c:when>
                                <c:otherwise>
                                     <td>undefined</td>
                                </c:otherwise>
                            </c:choose>
                            <c:choose>
                                <c:when test="${!empty busAgent.readProcessingEvent }">
                                     <td>${busAgent.readProcessingEvent.name} </td>
                                </c:when>
                                <c:otherwise>
                                     <td>undefined</td>
                                </c:otherwise>
                            </c:choose>
                            <c:choose>
                                <c:when test="${!empty busAgent.writeInitEvent }">
                                     <td>${busAgent.writeInitEvent.name} </td>
                                </c:when>
                                <c:otherwise>
                                     <td>undefined</td>
                                </c:otherwise>
                            </c:choose>
                            <c:choose>
                                <c:when test="${!empty busAgent.writeProcessingEvent }">
                                     <td>${busAgent.writeProcessingEvent.name} </td>
                                </c:when>
                                <c:otherwise>
                                     <td>undefined</td>
                                </c:otherwise>
                            </c:choose>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>
        
<%--         <div class="span9">
            <h4>信息显示页面</h4>
            <table class="table table-hover table-striped table-bordered">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>总线数据区</th>
                        <th>外部系统</th>
                        <th>响应函数</th>
                    </tr>
                </thead>
                <tbody>
                    <c:forEach items="${busAgents}" var="busAgent"  varStatus="status">
                        <tr>
                            <td>${status.count}</td>
                            <td>${busAgent.busZone}</td>
                            <td>${busAgent.outerSystem}</td>
                            <td>${busAgent.event.visibility} ${busAgent.event.name} </td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div> --%>
        
        <div class="span9">
            <h4>控制响应</h4>
            <table class="table table-hover table-striped table-bordered">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>名称</th>
                        <th>页面</th>
                        <th>事件</th>
                        <th>可见性</th>
                    </tr>
                </thead>
                <tbody>
                    <c:forEach items="${controlResponses}" var="controlResponse"  varStatus="status">
                        <tr>
                            <td>${status.count}</td>
                            <td>${controlResponse.name}</td>
                            <td>${controlResponse.pageUri}</td>
                            <td>${controlResponse.eventUri}</td>
                            <td>${controlResponse.visibility}</td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>
	</c:when>
</c:choose>

<div class="modal hide fade" id="add-entity-zone-panel">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal"
			aria-hidden="true">&times;</button>
		<h3>添加实体数据区</h3>
	</div>
	<div class="modal-body">
		<div class="control-group">
			<label class="control-label" for="entity_zone_category">类别</label>
			<div class="controls">
				<select name="entityZoneCategoryId" id="entity-zone-category">
					<c:forEach items="${entityZoneCategorys }" var="entityZoneCategory">
						<option value="${entityZoneCategory.id}">
							<c:out value="${entityZoneCategory.name}"></c:out>
						</option>
					</c:forEach>
				</select>
			</div>
		</div>
	</div>
	<div class="modal-footer">
		<button class="btn" id="add-entity-zone-btn">
			<i class="icon-ok"></i>&nbsp;确定
		</button>
		<button class="btn" data-dismiss="modal" aria-hidden="true">
			<i class="icon-remove"></i>&nbsp;取消
		</button>
	</div>
</div>

<script type="text/javascript">
function addEntityZone(entityUri, name, size, projectId, categoryId){
    jQuery.ajax( {     
        type : 'POST',     
        contentType : 'application/json',
        data: JSON.stringify({size:size,name:name, entityUri:entityUri}),
        dataType: "json",
        url : '/ManEnv/entityZone/' + projectId + "/add/" + categoryId,   
        success : function(data) {  
            if(data.result == "true"){
                alert("添加成功!"); 
            }else{
                alert("添加失败!");
            }
        },     
        error : function() {alert("内部错误!"); }
    });
};

function addDisplayZone(entityUri, name, size, projectId){
    jQuery.ajax( {     
        type : 'POST',     
        contentType : 'application/json',
        //data: {entityUri:entityUri, name:name, size:size},
        data: JSON.stringify({size:size,name:name, entityUri:entityUri}),
        dataType: "json",
        url : '/ManEnv/displayZone/' + projectId + "/add",   
        success : function(data) {  
            if(data.result == "true"){
                alert("添加成功!"); 
            }else{
                alert("添加失败!");
            }
        },     
        error : function() {alert("内部错误!"); }
    });
}

function showEntityZoneAddPanel(entityUri, name, size, projectId){
	 $("#add-entity-zone-panel").modal({
	     "show" : true,
	     "backdrop" : false
	 });
	 $("#add-entity-zone-btn").unbind("click");
	 $("#add-entity-zone-btn").click(function(){
		  var ezcId = $("#entity-zone-category").val();
		     if (ezcId == null || ezcId == ""){
		         alert("请选择实体数据区的类别！");
		         return;
		     }
		 addEntityZone(entityUri, name, size, projectId, ezcId);
		 //alert(entityUri + "," + name + "," + size + "," + projectId + "," + ezcId);
		 $("#add-entity-zone-panel").modal("hide");
	 });
}
</script>
<script src="<c:url value='/js/jquery.form.js'/>"></script>
<script src="<c:url value='/js/project.js'/>"></script>


